import json
import os
from os import path


class FilePipeline(object):
    def __init__(self):
        self.file = None
        self.all_data = []

    #     d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
    #     self.file = open(path.join(d, 'file/items.json'), 'w', encoding='utf-8')

    def open_spider(self, spider):
        d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
        self.file = open(path.join(d, 'file/items.json'), 'w', encoding='utf-8')

    def close_spider(self, spider):
        line = json.dumps(self.all_data, ensure_ascii=False)
        self.file.write(line)
        self.file.close()
        print('总行数：' + str(len(self.all_data)))

    def process_item(self, item, spider):
        self.all_data.append(dict(item))

        return item
